package com.lanren.familybookapp.ui.me.view;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hjq.toast.ToastUtils;
import com.lanren.common.helper.glidehelp.GlideHelper;
import com.lanren.common.utils.AppUtils;
import com.lanren.common.utils.StringUtils;
import com.lanren.common.utils.Utils;
import com.lanren.common.utils.event.C;
import com.lanren.common.widget.SettingBar;
import com.lanren.common.widget.TitleBarView;
import com.lanren.common.widget.image.RoundedImageView;
import com.lanren.common.widget.statusbar.ImmersionBar;
import com.lanren.easyeventbus.Event;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.base.BaseUiLazyFragment;
import com.lanren.familybookapp.ui.me.interfaces.IMeModel;
import com.lanren.familybookapp.ui.me.presenter.MePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ClassName MeFragment
 * @Description TODO 个人资料
 * @Author chongheng.wang
 * @Date 2019/10/5 16:53
 * @Version 1.0
 */
public class MeFragment extends BaseUiLazyFragment<MePresenter> implements IMeModel.IMeView {

    @BindView(R.id.title_view)
    TitleBarView titleView;
    @BindView(R.id.user_avatar)
    RoundedImageView userAvatar;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_book_time)
    TextView userBookTime;
    @BindView(R.id.user_oneself)
    ImageView userOneself;
    @BindView(R.id.user_book_manager)
    SettingBar userBookManager;
    @BindView(R.id.user_book_member)
    SettingBar userBookMember;
    @BindView(R.id.user_book_remind)
    SettingBar userBookRemind;
    @BindView(R.id.user_book_settings)
    SettingBar userBookSettings;
    @BindView(R.id.wb_refresh)
    SmartRefreshLayout wbRefresh;

    public static MeFragment newInstance() {
        return new MeFragment();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_me;
    }

    @Override
    public MePresenter createPresenter() {
        return new MePresenter();
    }

    @Override
    protected void setStatusBar() {
        mImmersionBar = ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    protected void initFragment() {
        initView();
        initData();
        initEvent();
    }


    public void initView() {
        wbRefresh.setEnableLoadMore(false);
        if (!AppUtils.isDebug()) {
            userBookMember.setVisibility(View.GONE);
            userBookRemind.setVisibility(View.GONE);
        }
    }

    @SuppressLint("SetTextI18n")
    public void initData() {
        setData();
    }

    public void initEvent() {
        wbRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getUserInfo();
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void setData() {
        GlideHelper.setImageResourceMemoryCache(getActivity(), userAvatar, AppUserHelper.getInstance().getUser().getUserAvatar(), R.mipmap
                .icon_app, true, DiskCacheStrategy.NONE);
        userName.setText(StringUtils.replaceNullByPstr(AppUserHelper.getInstance().getUser().getUserName(), "还木有昵称"));
        if (AppUserHelper.getInstance().getUser().getUserBookkeepingTime() != null) {
            userBookTime.setText("已记账" + AppUserHelper.getInstance().getUser().getUserBookkeepingTime() + "天，积蛙步以至千里！");
        } else {
            userBookTime.setText("没有记录哦~~");
        }
    }

    @Override
    protected void onRestart() {
    }

    @Override
    protected boolean registEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        switch (event.getCode()) {
            case C.EventCode.REFRESH_USER_INFO:
                setData();
                break;
        }
    }

    @OnClick({R.id.user_oneself, R.id.user_book_manager, R.id.user_book_member, R.id.user_book_remind, R.id.user_book_settings})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_oneself:
                if (Utils.isFastClick(view)) {
                    startActivity(OneselfActivity.class);
                }
                break;
            case R.id.user_book_manager:
                if (Utils.isFastClick(view)) {
                    startActivity(BookManagerActivity.class);
                }
                break;
            case R.id.user_book_member:
                break;
            case R.id.user_book_remind:
                break;
            case R.id.user_book_settings:
                if (Utils.isFastClick(view)) {
                    startActivity(SettingsActivity.class);
                }
                break;
        }
    }

    @Override
    public void showError(int code, String msg) {
        super.showError(code, msg);
    }

    @Override
    public void onError(String data) {
        ToastUtils.show(data);
    }

    @Override
    public void onSuccess() {
        wbRefresh.finishRefresh();
        setData();
    }
}
